/**
 * Created by Administrator on 2017/5/9.
 */

//判断是否是JSON字符串
var isJSONstring=function(str) {
    if (typeof str == 'string') {
        try {
            JSON.parse(str);
            console.log(str,true);
            return true;
        } catch(e) {
            console.log(e,false);
            return false;
        }
    }
    console.log(str+'不是一个字符串')
};

//判断是否是JSON对象
var isJSONobject=function(obj){
    if(typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length){
        console.log(obj+"true");
        return true;
    }else {
        console.log(obj+"false");
        return false;
    }
};

function isEmptyObject(obj){
    if (typeof obj === "object" && !(obj instanceof Array)){
        var hasProp = true;
        for (var item in obj){
            hasProp = false;
            break;
        }
        return hasProp;
    }else {
        return false;
    }
}
// 箭头变换工具
var chevronstatechange = function(id){
    var chevron = document.getElementById(id);
    if(chevron.className == "icon-chevron-up"){
        chevron.className = "icon-chevron-down"
    }else {
        chevron.className = "icon-chevron-up"
    }
};

//JSON字符串转换成对象工具
var string_to_object=function(objectarray,keyname){
     if (objectarray instanceof Array){
         for (var i=0;i<objectarray.length;i++){
             var item = objectarray[i];
             if (isJSONstring(item[keyname])){
                 var jsonObject = JSON.parse(item[keyname]);
                 item[keyname]=jsonObject;
             }else {console.log(item[keyname]+"不是一个JSON字符串")}
         }
     }
     else {
        if (isJSONstring(objectarray[keyname])){
            objectarray[keyname]=JSON.parse(objectarray[keyname])
        }else {console.log(objectarray[keyname]+"不是一个JSON字符串")}
     }
};

//对象转换成JSON字符串工具
var object_to_string=function(object,keyname){
    if (object instanceof Array){
        for (var i=0;i<object.length;i++){
            if(isJSONobject(object[i][keyname])){
                object[i][keyname]=JSON.stringify(object[i][keyname]);
            }else {console.log(object[i][keyname]+"不是一个JSON对象")}
        }
    }
    else {if(isJSONobject(object[keyname])){
        object[keyname]=JSON.stringify(object[keyname]);
    }else {console.log(object[keyname]+"不是一个JSON对象")}
    }
};
//构造pagenum_list函数
function create_pagenum_list(page_sum,page_num){
    var pagenum_list=[];
    if(page_sum<7){
        for (var i= 1;i<=page_sum;i++){
            pagenum_list.push(i)
        }
    }else {
        if (page_sum-page_num>=5){
            if (page_num<3){
                pagenum_list=[1,2,3,4,5,"...",page_sum-1,page_sum];
            }else {
                pagenum_list=[page_num-2,page_num-1,page_num,page_num+1,page_num+2,"...",page_sum-1,page_sum]
            }
        }else {
            pagenum_list=[1,2,"...",page_sum-4,page_sum-3,page_sum-2,page_sum-1,page_sum];
        }
    }
    return pagenum_list
}

//元素居中函数(通过margin属性，将id为elementid的元素)
var margin_center=function(elementid){
    setInterval(function () {
        var element=document.getElementById([elementid]);
        var element_style= document.defaultView.getComputedStyle(element,null);
        element.style["margin-left"]="calc((100% - "+element_style.width+") / 2)";
        console.log(5)
    },100);
};
